package site.marqstree.kotlin.library.good.ui.activity.detail

import android.os.Bundle
import android.view.Gravity
import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.aty_good_detail.*
import q.rorbin.badgeview.Badge
import q.rorbin.badgeview.QBadgeView
import site.marqstree.kotlin.library.base.data.net.observer.RxObserver
import site.marqstree.kotlin.library.base.ui.activity.BaseARouterActivity
import site.marqstree.kotlin.library.base.ui.widget.banner.ImageBannerAdapter
import site.marqstree.kotlin.library.base.util.rxbus.RxBus
import site.marqstree.kotlin.library.good.R
import site.marqstree.kotlin.library.good.ui.activity.detail.sku.event.EventAddCart
import site.marqstree.kotlin.library.good.ui.activity.detail.tabs.TabsAdapter
import site.marqstree.kotlin.library.good.ui.bean.GoodDetailBean
import site.marqstree.kotlin.library.good.ui.service.GoodService


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.good.ui.activity.detail
 * 文件名: ActivityGoodDetail
 * 创建者: marqstree
 * 创建时间: 2020/3/11 15:44
 * 描述: TODO
 */
@Route(path = "/good/activity/gooddetail")
class AtyGoodDetail :BaseARouterActivity(),
    AppBarLayout.OnOffsetChangedListener {

    @Autowired
    @JvmField var mGoodId: Int? = 0

    @Autowired
    @JvmField var mService: GoodService? = null

    private var mGoodDetailBean: GoodDetailBean? = null

    //购物车数量标记
    private lateinit var mCartCountBadge: QBadgeView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.aty_good_detail)

        //获取数据并刷新UI
        initView()

        //设置滑动事件
        mAppBarLayout.addOnOffsetChangedListener(this)
    }

    private fun initTabAndViewPager() {
        //绑定TabLayout、ViewPager
        mViewpager.adapter = TabsAdapter(this,
                                mGoodDetailBean!!)
        TabLayoutMediator(mTab, mViewpager,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                // tab:当前处于选中状态的Tab对象
                // position:当前Tab所处的位置
                tab.text = mGoodDetailBean!!.tabs[position].name
            }
        ).attach() // 不要忘记，否则没效果
    }

    private fun initView() {
        //获取数据
        mService?.getGoodDetail(mGoodId!!)
            ?.subscribe(object : RxObserver<GoodDetailBean>(){
                override fun onNext(data: GoodDetailBean) {
                    super.onNext(data)
                    mGoodDetailBean = data
                    //初始化banner
                    initBanner()
                    //初始化Tab页和ViewPager
                    initTabAndViewPager()
                }
            })

        mCartCountBadge = QBadgeView(this)
        mCartCountBadge.bindTarget(mShopCartIcon)
            // 图片徽章的位置
            .setBadgeGravity(Gravity.TOP or Gravity.END)
            // 偏移量，x方向，y方向，是否是dp值
            .setGravityOffset(40f, 10f, false)
            .setBadgeNumber(0)
        RxBus.observe<EventAddCart>()
            .subscribe{
                mCartCountBadge.setBadgeNumber(it.mCount)
            }
    }

    private fun initBanner(){
        mBanner.setIndicator(CircleIndicator(this))
        mBanner.adapter = ImageBannerAdapter(mGoodDetailBean!!.banners,this)
        mBanner.start()
    }

    //滑动时，显隐toolbar
    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        //获取滑动范围
        val maxScrollSize = appBarLayout!!.totalScrollRange

        //计算当前滑动百分比
        val percentage: Float = Math.abs(verticalOffset) * 1.0f / maxScrollSize

        //根据滑动程度，动态调整toolbar的显隐
        //滑到100%，alpha值1.0f,显示
        //滑到0%，alpha值0.0f,隐藏
        mToolbar.alpha = percentage
    }

}